package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.BookingPagesBean;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;

import com.itheima.pojo.Address;
import com.itheima.pojo.BookingOrder;
import com.itheima.pojo.Setmeal;
import com.itheima.service.AddressService;
import com.itheima.service.BookingOrderService;

import com.itheima.service.SetMealService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 后台管理预约管理
 */

@RestController
@RequestMapping("/bookingorder")
public class BookingOrderController {
    @Reference
    private BookingOrderService bookingOrderService;
    @Reference
    private SetMealService setMealService;
    @Reference
    private AddressService addressService;

    //查询所有数据
    @RequestMapping("/findAll")
    public Result findAll(){
        return null;
    }


    /**
     * 根据订单id来查询此预约订单所有信息
     *查询订单order的id 作为唯一的标识
     * */
    @RequestMapping("/findOrderById")
    public Result findOrderById(Integer id){
        try{
           Map<String,Object> map = bookingOrderService.findOrderById(id);
            //查询
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch(Exception e){
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    //分页展示数据信息
    //当用户再页面点击后管按钮时,前端钩子函数绑定分页查询的方法,直接调用findPage()方法,将所有数据显示到页面
    //需要显示的内容:
        //序号(id)丶会员姓名丶电话号码丶预约日期丶预约类型丶预约状态(开关控件)丶套餐名称丶操作(编辑丶删除)按钮
    //已经搞定
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody BookingPagesBean queryPageBean){
        return bookingOrderService.findPage(queryPageBean);
    };

   /* @RequestMapping("/findPage")
    public Result findPage(@RequestBody Map map){
        try {
            PageResult pageResult = bookingOrderService.findPage(map);
            //添加判断
            if (pageResult.getTotal() == 0) {
                return new Result(true,"没有查询到任何结果",pageResult);
            }
            return new Result(true,"查询成功",pageResult);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"网络错误");
        }
    };*/

    //查询所有的机构地址
    @RequestMapping("/findAddressAll")
    public Result findAddressAll(){
        try{
            //获取所有的医院地址
               List<Address> list = addressService.findAddressAll();
               //把获取到的医院地址list集合,用参数返回给页面
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS,list);
        }catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    /**
     *     //查询请求关联的套餐id集合
     * @param id
     * @return
     */
    @RequestMapping("/findSetmealIdsByOrderId")
    public Result findSetmealIdsByOrderId(Integer id) {
        //获取所有套餐id集合
        try {
            List<Integer> idList = bookingOrderService.findSetmealIdsByOrderId(id);
            return new Result(true,"查询关联套餐id成功",idList);
        } catch (Exception e) {
            return new Result(false,"查询关联套餐id失败");
        }
    }

    //查询套餐信息数据
    @RequestMapping("/findAllSetmeal")
    public Result findAllSetmeal(){
        try {
            //获取到所有的套餐集合 ?? 怎么返回给页面
            List<Setmeal> all = setMealService.findAllSetmeal();
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,all);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    //修改预约信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map, Integer setmealId){
        try{
            //直接调用service层的edit方法
            bookingOrderService.edit(map,setmealId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_BOOKINGORDER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_BOOKINGORDER_SUCCESS);
    }

    //新增预约 (表单信息校验成功是前提)
//有返回值,要返回给结果给页面响应页面
//传递过来的参数丶 用map集合接收,因为对象接收不了
    @RequestMapping("/addPhoneOrder")
    public Result addPhoneOrder(@RequestBody Map map) throws Exception {
        //System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwwww"+map);
        //System.out.println(bookingOrder);
        //System.out.println("wwwwwwwwwww"+addressId);

        //调用service层
        return bookingOrderService.addPhoneOrder(map);
    }



    //删除方法
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            bookingOrderService.delete(id);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_BOOKINGORDER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_BOOKINGORDER_SUCCESS);
    }

    /**
     * 已到诊和未到诊的开关控件
     */
    @RequestMapping("/statusEdit")
    public Result statusEdit(@RequestBody Map map){
        try{
            bookingOrderService.statusEdit(map);
            return new Result(true,"预约状态修改成功了");
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"预约状态修改失败昂");
        }
    }


    //清除方法 清除所有复选框的查询记录
    //直接在前端页面进行了.....
    /**
     * 查询方法
     *  根据会员姓名查/根据电话号码查
     *  根据预约类型查/预约状态查/套餐名称查
     */

    @RequestMapping("/findPhoneOrName")
    public PageResult findPhoneOrName(){
        return null;
    }


}
